package com.wpf.system.common.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

public class UploadUtils {

    public static String uploadAvatar(MultipartFile file,String id,String reAvatar) {
        //文件夹
        ApplicationHome applicationHome = new ApplicationHome();
        String filePath = applicationHome.getDir() + "\\src\\main\\resources\\static\\upload\\img\\" + id;
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        //删除旧头像文件
        if(StrUtil.isNotBlank(reAvatar)){
            String deletePath =  applicationHome.getDir() + "\\src\\main\\resources\\" + reAvatar.replace("/","\\");
            File deleteFile = new File(deletePath);
            if(deleteFile.exists()){
                deleteFile.delete();
            }
        }

        //文件名
        //前端传送完整的文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String randomFilename = "avatar" + id + "_" + RandomUtil.randomNumbers(6) + "_" + System.currentTimeMillis();

        //保存文件到本地的完整路径
        String fileUrl = filePath + "\\" + randomFilename+suffix;

        try {
            file.transferTo(new File(fileUrl));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //保存文件网络路径到数据库
        return "/upload/img/" + id+"/"+randomFilename+suffix;
    }

    public static String uploadPictures(MultipartFile file,String bid) {
        //文件夹
        ApplicationHome applicationHome = new ApplicationHome();
        String filePath = applicationHome.getDir() + "\\src\\main\\resources\\static\\upload\\img\\" + bid;
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        //文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String randomFilename = "pic" + bid + "_" + RandomUtil.randomNumbers(6) + "_" + System.currentTimeMillis();

        //保存文件到本地的完整路径
        String fileUrl = filePath + "\\" + randomFilename+suffix;

        try {
            file.transferTo(new File(fileUrl));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //保存文件网络路径到数据库
        return "/upload/img/" + bid+"/"+randomFilename+suffix;
    }

    public static String uploadNotice(MultipartFile file,String bid) {
        //文件夹
        ApplicationHome applicationHome = new ApplicationHome();
        String filePath = applicationHome.getDir() + "\\src\\main\\resources\\static\\upload\\notice\\" + bid;
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        //文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        String randomFilename = "pic" + bid + "_" + RandomUtil.randomNumbers(6) + "_" + System.currentTimeMillis();

        //保存文件到本地的完整路径
        String fileUrl = filePath + "\\" + randomFilename+suffix;

        try {
            file.transferTo(new File(fileUrl));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回相对路径
        return "/upload/notice/"+bid+"/"+randomFilename+suffix;
    }

    //删除图片
    public static void deletePictures(String url,String serverAddress) {
        String reUrl = url.replace(serverAddress,"static");
        ApplicationHome applicationHome = new ApplicationHome();
        String deletePath =  applicationHome.getDir() + "\\src\\main\\resources\\" + reUrl.replace("/","\\");

        File deleteFile = new File(deletePath);
        if(deleteFile.exists()){
            deleteFile.delete();
        }
    }

    //检查文件夹是否为空
    public static void deleteDirectory(String bid) {
        ApplicationHome applicationHome = new ApplicationHome();
        File dirFile = new File(applicationHome.getDir() + "\\src\\main\\resources\\static\\upload\\img\\"+bid);
        if(dirFile.isDirectory()){
            if(dirFile.list().length==0){
                dirFile.delete();
            }
        }
    }

    public static Boolean deleteAllFiles(String bid,String dir) {
        ApplicationHome applicationHome = new ApplicationHome();
        File file = new File(applicationHome.getDir() + "\\src\\main\\resources\\static\\upload\\"+dir+"\\"+bid);
        return deleteFile(file);
    }

    public static Boolean deleteFile(File file) {
        //判断文件不为null或文件目录存在
        if (file == null || !file.exists()) {
            return false;
        }
        //获取目录下子文件
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f : files) {
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()) {
                //递归删除目录下的文件
                deleteFile(f);
            } else {
                //文件删除
                f.delete();
            }
        }
        //文件夹删除
        file.delete();
        return true;
    }
}
